package com.myapp.ruyixing.ui;

import static com.myapp.ruyixing.http.RYHttpClient.getOthersSigninList;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.MySigninList;
import com.myapp.ruyixing.model.MySigninList.Signin;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.LoginHelper;

public class RegistrationActivity extends Activity implements
		OnItemClickListener {

	private MyAdapter mAdapter;
	private ArrayList<Signin> mSigninList;
	private ListView mListView;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				MySigninList list = (MySigninList) o;
				onLoadingDataFinished(true, list);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	private void onLoadingDataStarted(String bduss, int pn, int rn) {
		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);
		RYHttpClient.getMySigninList(this, mHttpResponseHandler, bduss, pn, rn);
	}

	private void onLoadingDataStarted2(String userid, int pn, int rn) {
		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);
		getOthersSigninList(this, mHttpResponseHandler, userid, pn, rn);
	}

	private void onLoadingDataFinished(boolean succeed, MySigninList list) {
		((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
		if (succeed && list != null) {
			if (list.items != null && list.items.size() > 0) {
				mSigninList.clear();
				mSigninList.addAll(list.items);
				mAdapter.notifyDataSetChanged();
			} else {
				showToast("无签到数据哦！");
			}
		} else {
			showToast("加载签到列表失败！");
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registration);
		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		mListView = (ListView) findViewById(R.id.list);
		mSigninList = new ArrayList<Signin>();
		mAdapter = new MyAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);

		String userId = getIntent().getStringExtra("userid");
		if (TextUtils.isEmpty(userId)) {
			onLoadingDataStarted(LoginHelper.getInstance().getBDUSS(), 0, 50);
		} else {
			onLoadingDataStarted2(userId, 0, 50);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
		ShopDetailActivity.startShopDetailActivity(this,
				mSigninList.get(arg2).shopid);
	}

	private class MyAdapter extends BaseAdapter {

		private class ViewHolder {
			public TextView mShopName;
			public TextView mDateTime;
			public TextView mDiscription;
			public ImageView mScoreView;
			public TextView mScoreText;
		}

		private RequestQueue mQueue;
		private ImageLoader mImageLoader;

		public MyAdapter() {
			super();
			mQueue = Volley.newRequestQueue(RegistrationActivity.this);
			mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		}

		@Override
		public int getCount() {
			return mSigninList == null ? 0 : mSigninList.size();
		}

		@Override
		public Object getItem(int position) {
			return mSigninList == null ? null : mSigninList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			Signin item = (Signin) getItem(position);
			if (item == null) {
				return convertView;
			}
			if (convertView == null) {
				convertView = LayoutInflater
						.from(RegistrationActivity.this)
						.inflate(R.layout.registration_list_item, parent, false);
				holder = new ViewHolder();
				holder.mDateTime = (TextView) convertView
						.findViewById(R.id.text_date);
				holder.mDiscription = (TextView) convertView
						.findViewById(R.id.text_discription);
				holder.mScoreText = (TextView) convertView
						.findViewById(R.id.text_score);
				holder.mShopName = (TextView) convertView
						.findViewById(R.id.text_shop_name);
				holder.mScoreView = (ImageView) convertView
						.findViewById(R.id.img_score);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.mScoreView.setImageLevel(item.score);
			holder.mScoreText.setText(item.score + "");
			if (!TextUtils.isEmpty(item.content)) {
				holder.mDiscription.setText(item.content);
			}
			if (!TextUtils.isEmpty(item.title)) {
				holder.mShopName.setText(item.title);
			}
			if (!TextUtils.isEmpty(item.datetime)) {
				holder.mDateTime.setText(item.datetime);
			}
			return convertView;
		}
	}

}
